package polimi.sc.expoquest.screen.gossip;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Random;

import polimi.sc.expoquest.QuestApplication;
import polimi.sc.expoquest.R;
import polimi.sc.expoquest.screen.CameraActivity;
import polimi.sc.expoquest.screen.DashboardActivity;
import polimi.sc.expoquest.storage.datasources.GossipDataSource;
import polimi.sc.expoquest.storage.model.Gossip;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.view.Menu;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.Toast;

public class NewGossipActivity extends Activity {
	/**
	 * The gossip related datasource
	 */
	private GossipDataSource GossipDS;
	public static final int CAMERA_CODE = 99;
	private String fileName;
	
	private String PictureURIs []={"dragonfruit.jpg","hotpotchina.jpg","lasagna.jpg","lichi.jpg","lulo.jpg","melon.jpg"};
	private int random;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_new_gossip);
		
		//initial
		random=0;
		fileName="";
		
		//button publish new gossip
		 findViewById(R.id.publishnewgossip).setOnClickListener(
					new View.OnClickListener() {
						@Override
						public void onClick(View view) {
							
							publishGossip();
						}
					});
		 
		//button Takepic
		 findViewById(R.id.takepic).setOnClickListener(
					new View.OnClickListener() {
						@Override
						public void onClick(View view) {
							
							//takePic();
							takepicturePressed(view);
						}
					});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_new_gossip, menu);
		return true;
	}
	
	public void homepagebtn_click(View v){
		  Intent dashbordIntent = new Intent(this, DashboardActivity.class);
		        startActivity(dashbordIntent);
		 }

	public void publishGossip()
	{
		//get the input
		String user_input="";
		EditText gossipcontent= (EditText) findViewById(R.id.newgossipcontent);
		user_input= gossipcontent.getText().toString();
		if(!user_input.isEmpty())
		{
			//get the user
			QuestApplication questApp = (QuestApplication)getApplicationContext();
			int userID = questApp.getUserID();
			//insert into database
			if(GossipDS== null)
			GossipDS = new GossipDataSource(this);
			GossipDS.open();
			Gossip newGossip=GossipDS.insert(user_input, fileName, userID);
			GossipDS.close();
			
			//refresh
			gossipcontent.setText("");
			fileName="";
			//clearpic();
			Toast toast = Toast.makeText(this, "gossip added successfully" , Toast.LENGTH_LONG);
			toast.show();
			//go to the gossip main
			Intent gossipIntent = new Intent(this, GossipMainActivity.class);
			gossipIntent.putExtra("DELIVERDGOSSIP", newGossip.getId());
	        startActivity(gossipIntent);
			
		}
		
	}
	
	private void takePic()
	{
		//choose the pic
		Random generator = new Random();
		int totalpic=PictureURIs.length;
		random=0;
		try {
			random = generator.nextInt(totalpic);
		}
		catch(IllegalArgumentException e){}
		
		//show the pic of the gossip
			 ImageView image=(ImageView)findViewById(R.id.takepic);
				try {
					Drawable d;				
					d = loadImage(PictureURIs[random]);
					image.setImageDrawable(d);
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
									
	}
	
	private void clearpic()
	{
		ImageView image=(ImageView)findViewById(R.id.takepic);							
	    image.setImageDrawable(getResources().getDrawable(R.drawable.cammera));
		
	}
	
	
	private Drawable loadImage(String path) throws IOException
	{
		// get input stream
		InputStream ims = getAssets().open(path);
		// load image as Drawable
		Drawable d = Drawable.createFromStream(ims, null);
		return d;
	}
	
	public void takepicturePressed(View v)
	{
		Intent cameraIntent = new Intent(this, CameraActivity.class);		
		startActivityForResult(cameraIntent, CAMERA_CODE);
	}
	
	@Override
	public void onActivityResult(int requestCode , int resultCode, Intent data)
	{
		super.onActivityResult(requestCode, resultCode, data);
			if (requestCode == CAMERA_CODE)
			{
				if (resultCode == Activity.RESULT_OK)
				{	
					fileName = data.getStringExtra("fileName");
					File f = new File(fileName);	
					Toast.makeText(this, fileName, Toast.LENGTH_LONG).show();
					
					try {
						InputStream ims = new FileInputStream(f);
						Drawable d = Drawable.createFromStream(ims, null);
						ImageView imgpic = (ImageView) findViewById(R.id.gossippicframe);
						imgpic.setVisibility(View.VISIBLE);
						imgpic.setImageDrawable(d);
						ImageButton imgCamera = (ImageButton) findViewById(R.id.takepic);
						imgCamera.setVisibility(View.INVISIBLE);
					} catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}				
			}
			
	}

}
